"""
Case Type   : pagehack工具测试
Case Name   : pagehack 解析gin索引， -t 为ubtree索引类型，合理报错
Create At   : 2025-10-25
Owner       : chen
Description :
    1.建表
    2.创建 gin 索引
    3.插入测试数据 (1000行)
    4.查询索引文件路径
    5.解析
Expect      :
    1.成功
    2.成功
    3.成功
    4.成功
    5.合理报错
History     :
"""

import os
import unittest
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from yat.test import Node
from yat.test import macro

log = Logger()
primary_sh = CommonSH('PrimaryDbUser')

class Tools_Pagehack_Case0031(unittest.TestCase):
    def setUp(self):
        log.info(f'--------{os.path.basename(__file__)} start--------')
        self.constant = Constant()
        self.pri_node = Node('PrimaryDbUser')
        self.table_name = 't_pagehack_0031'
        self.index_name = 'i_pagehack_0031'

    def test_pagehack(self):
        text = '-------------step1: 建表; expect: 成功-------------'
        log.info(text)
        msg = primary_sh.check_connection_status(self.constant.START_STATUS_MSG)
        self.assertTrue(msg)
        create_table = f'''drop table if exists {self.table_name};
            create table {self.table_name} (
                id serial primary key,
                name varchar(50),
                description text,
                points point,
                tags text[],
                score double precision,
                created_at timestamp default current_timestamp
            ) with (orientation=row);'''
        res = primary_sh.execut_db_sql(create_table)
        log.info(res)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, res)

        text = '-------------step2: 创建 gin 索引; expect: 成功-------------'
        log.info(text)
        create_index = f'create index {self.index_name} on {self.table_name}' \
            f' using gin(tags);'
        res = primary_sh.execut_db_sql(create_index)
        log.info(res)
        self.assertIn(self.constant.CREATE_INDEX_SUCCESS, res)

        text = '-------------step3: 插入测试数据; expect: 成功-------------'
        log.info(text)
        insert_sql = f'''insert into {self.table_name} (name, description, 
            points, tags, score)
            select
                'product_' || n::text,
                'description for product ' || n::text,
                point(random() * 100, random() * 100),
                array[('tag' || (floor(random() * 10)::int)::text), 
                ('category' || (floor(random() * 5)::int)::text)],
                random() * 100
            from generate_series(1, 1000) as n;'''
        res = primary_sh.execut_db_sql(insert_sql)
        log.info(res)
        self.assertIn('INSERT 0 1000', res)
        
        text = '-------------step4: 查询索引文件路径; expect: 成功-------------'
        log.info(text)
        select_sql = f"SELECT pg_relation_filepath('{self.index_name}');"
        res = primary_sh.execut_db_sql(select_sql)
        log.info(res)
        self.assertIn('(1 row)', res)
        index_path = str(res).splitlines()[-2].strip()

        text = '-------------step5: 解析; expect: 合理报错-------------'
        log.info(text)
        shell = f'source {macro.DB_ENV_PATH}; ' \
            f'cd {macro.DB_INSTANCE_PATH} && ' \
            f'pagehack -f {index_path} -t ubtree_pcr_index'
        log.info(shell)
        msg = self.pri_node.sh(shell).result()
        log.info(msg)
        fail_text = 'it is not ubtree index file, please choose correct type'
        self.assertIn(fail_text, msg)
        shell = f'source {macro.DB_ENV_PATH}; ' \
            f'cd {macro.DB_INSTANCE_PATH} && ' \
            f'pagehack -f {index_path} -t ubtree_index'
        log.info(shell)
        msg = self.pri_node.sh(shell).result()
        log.info(msg)
        self.assertIn(fail_text, msg)

    def tearDown(self):
        text = '------------------清理环境; expect: 成功------------------'
        log.info(text)
        drop_sql = f'''drop index if exists {self.index_name};
            drop table if exists {self.table_name};'''
        res = primary_sh.execut_db_sql(drop_sql)
        log.info(res)
        self.assertIn(self.constant.DROP_INDEX_SUCCESS_MSG, res)
        self.assertIn(self.constant.DROP_TABLE_SUCCESS, res)